package global

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"time"
)

var RedisDb *redis.Client

func init() {
	addr := AppConfig.Section("redis").Key("addr").String()
	pwd := AppConfig.Section("redis").Key("pwd").String()
	state := AppConfig.Section("redis").Key("state").String()
	dbNum, _ := AppConfig.Section("redis").Key("db").Int()
	if state == "on" {
		RedisDb = redis.NewClient(&redis.Options{
			Addr:         addr,
			Password:     pwd,   // no password set
			DB:           dbNum, // use default DB
			PoolSize:     16,
			MinIdleConns: 4,
			MaxConnAge:   time.Minute * 5,
		})
		_, err1 := RedisDb.Ping(context.Background()).Result()
		if err1 != nil {
			fmt.Println("redis连接失败...", err1)
			return
		}
		fmt.Printf("%-32s%-32s\n", "redis connect", "OK")
	}
}
